package com.tuniu.agents.command.match;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Regular expression matcher, matches regular expressions
 */
public class PatternMatch extends AbstractMatch {

    /**
     * Regular expression pattern, used to match serial numbers
     */
    protected final Pattern pattern;

    /**
     * Constructor
     *
     * @param regex Regular expression
     */
    public PatternMatch(String regex) {
        this.pattern = Pattern.compile(regex, Pattern.DOTALL);
    }

    /**
     * Determines if the input matches this matcher
     *
     * @param input   User input
     * @param context Context parameters
     * @return Whether it matches
     */
    @Override
    public boolean matches(String input, Map<String, Object> params, Map<String, Object> context) {
        if (input == null || input.trim().isEmpty()) {
            return false;
        }

        Matcher matcher = pattern.matcher(input.trim());
        if (!matcher.matches()) {
            return false;
        }

        return true;

    }

}
